package com.zhongwang.mms.module.half.controller;

import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.half.model.HInBillModel;
import com.zhongwang.mms.module.half.service.HBasicService;
import com.zhongwang.mms.module.half.service.FinishInWarService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 入库单
 */
@Controller
@RequestMapping("/half/halWarManage/finishWar")
public class FinishInWarController {
    //页面文件路径
    private String prefix = "half/halWarManage/finishWar";
    @Autowired
    private FinishInWarService finishInWarService;
    @Autowired
    private HBasicService hBasicService;

    /**
     * 制作件入库单页面
     * @param model
     * @return
     */
    @GetMapping("/page")
    @RequiresPermissions({"halwar:halmakeinwar:view"})
    public String create(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("dept", hBasicService.getDept());
        model.addAttribute("warInfo", hBasicService.getFinishWarInfo());
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return prefix + "/list";
    }

    /**
     * 创建制作件入库单保存
     * @param hInBillModel
     * @return
     */
    @ResponseBody
    @PostMapping("/create")
    @RequiresPermissions({"halwar:halmakeinwar:edit"})
    public JsonResult create(@RequestBody HInBillModel hInBillModel) {

        return finishInWarService.saveHInBillModel(hInBillModel);
    }

    /**
     * 打印获取主单信息
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/printList")
    @RequiresPermissions({"halwar:halmakeinwar:print"})
    public JsonResult printList(@RequestParam("id") Integer id){

        return finishInWarService.selectHOrderById(id);

    }

    /**
     * 打印获取明细信息
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/printDetailList")
    @RequiresPermissions({"halwar:halmakeinwar:print"})
    public JsonResult printDetailList(@RequestParam("id") Integer id){

        return finishInWarService.selectHOrderDetailByOdrId(id);

    }
}
